package tech.wowo.controller;

import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import tech.wowo.mapper.UserMapper;
import tech.wowo.pojo.User;
import tech.wowo.service.UserService;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
@RestController
@RequestMapping("/user")
@Log4j
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/getUser")
    public Map<String, Object> getUser(HttpSession session){
        User  loginUser =  (User)session.getAttribute("user");
        Map<String, Object> result = new HashMap<>();
        result.put("message", "200");
        result.put("user", loginUser);
        return result;
    }


    @RequestMapping("/login")
    //    public Map<String, Object> login(String username, String password){
    public Map<String, Object> login(@RequestBody User user, HttpSession session){

        log.info("----UserController---login method--");
        Map<String, Object> result = new HashMap<>();

//        User user = userService.login(username, password);
        User loginUser = userService.login(user.getUsername(), user.getPassword());
        if(loginUser!=null){
            result.put("message", "200");
            session.setAttribute("user", loginUser);
        }else{
            result.put("message", "201");
        }

        result.put("user", loginUser);

        return result;
    }


    @RequestMapping("/add")
    public Map<String,Object>addUser(User user,MultipartFile file){
        log.info("----UserController---getUserListByName method--");
        Map<String,Object> result = new HashMap<>();

        if (!file.isEmpty()){
String imgName = UUID.randomUUID()+file.getOriginalFilename();
String path = "D:\\Server\\apache-tomcat-8.5.93-windows-x64\\apache-tomcat-8.5.93\\webapps\\voteImg\\userImg\\";
try{
    file.transferTo(new File(path+imgName));
}catch (IOException e){
    e.printStackTrace();
    result.put("message","202");
    return result;
}
user.setUserImg("/voteImg/userImg/"+imgName);
        }

       Integer i = userService.addUser(user);

        result.put("message","200");
        result.put("i",i);

        return result;
    }
    /**
     * 查询用户，要求传入用户名
     * @return
     */
    @GetMapping("/list")
    public Map<String, Object> getUserListByName(String username){
        log.info("----UserController---getUserListByName method--");
        Map<String, Object> result = new HashMap<>();

        User user = new User();
        user.setUsername(username);

        List<User> userList = userService.getUserListByName(user);

        result.put("message", "200");
        result.put("userList", userList);

        return result;
    }


}
